ARD2  RC2
Airbag Reference Demonstrator using MPC5604P
SM.c
Go to the documentation of this file.
00001 
00016 #include "derivative.h"
00017 #include "compile_options.h"
00018 #include "HAL.h"
00019 #include "SM.h"
00020 #include "Application_Globals.h"
00021 #include "Compile_options.h"
00022 /*
00023  ******************************************************************************
00024  * Constants
00025  ******************************************************************************
00026  */
00028 /* each function represents a state within the State machine */
00029 uint32_t (* const u32pfnState[])(void) =
00030 {
00031   &u32fnSMInit, &u32fnStateError, &u32fnStateAcquisition, 
00032   &u32fnStateDecision, &u32fnStateDeployment, &u32fnStateSystemTest, 
00033   &u32fnStateGUI};
00034 /*
00035  ******************************************************************************
00036  * Globals
00037  ******************************************************************************
00038  */
00042 uint16_t gu16SMCurrentState[2u];
00044 uint32_t gu32SMStateStatus;
00045 /*
00046  ******************************************************************************
00047  * u32SMLaunch
00048  ******************************************************************************
00049  */
00050 uint32_t u32fnSMLaunch(void)
00051 {
00052   /* Init current state indicators */
00053   gu16SMCurrentState[SM_PREVIOUS_STATE] = SM_STATE_SETUP;
00054   gu16SMCurrentState[SM_NEXT_STATE] = SM_STATE_SETUP;
00055   
00056   /* State-machine related globals that should be initialized before jumping in */
00057   gu32SMCycleStartTime = CLEAR;
00058   
00059   for(;;)
00060   {
00061     /* Call the next state */
00062 #ifdef DEBUGGING_NOW
00063     SIU.GPDO[9].B.PDO = 1;
00064 #endif
00065     gu32SMStateStatus = (*u32pfnState[gu16SMCurrentState[SM_NEXT_STATE]])();
00066     
00067 #ifdef DEBUGGING_NOW
00068     SIU.GPDO[9].B.PDO = 0;
00069 #endif
00070   }
00071   
00072   /* We should never reach this point */
00073   return (CLEAR);
00074 }
00075 /*
00076  ******************************************************************************
00077  * vfnSMWriteNextState
00078  ******************************************************************************
00079  */
00080 void vfnSMWriteNextState(uint16_t u16NextState)
00081 {
00082   /* Take the current state and move it to the back */
00083   gu16SMCurrentState[SM_PREVIOUS_STATE] = gu16SMCurrentState[SM_NEXT_STATE];
00084   
00085   /* Make the next step actually the passed variable */
00086   gu16SMCurrentState[SM_NEXT_STATE] = u16NextState;
00087   
00088   return;
00089 }
00090 /*
00091  ******************************************************************************
00092  * u32fnSMValidateCurrentState
00093  ******************************************************************************
00094  */
00095 uint32_t u32fnSMValidateCurrentState(uint16_t* pu16ValidStateList,
00096                                      uint16_t u16Size)
00097 {
00098   uint32_t u32Status;
00099   
00100   /* Init status to incorrect value */
00101   u32Status = SM_STATE_IS_EXECUTING;
00102   
00103   /* Test to verify previous state ended correctly */
00104   if(CLEAR == (gu32SMStateStatus & SM_STATE_IS_EXECUTING))
00105   {
00106     /* Init to next incorrect state */
00107     u32Status = SM_INVALID_SOURCE_STATE;
00108     
00109     /* We will loop through all our values */
00110     for(; u16Size > CLEAR; u16Size--)
00111     {
00112       if(gu16SMCurrentState[SM_PREVIOUS_STATE] == *(pu16ValidStateList++))
00113       {
00114         /* If the previous state is found on the list, we are good */
00115         u32Status = CLEAR;
00116         break;
00117       }
00118       else
00119       {
00120         /* Keep looking */
00121       }
00122     }
00123   }
00124   else
00125   {
00126     /* Get out */
00127   }
00128   return (u32Status);
00129 }
00130 /*
00131  ******************************************************************************
00132  * u32fnSMInit
00133  ******************************************************************************
00134  */
00135 //uint32_t u32fnSMInit(void)
00136 //{
00137 //  uint32_t u32Status;
00138 //  
00139 //  /* Verify that we can execute - Previous state ended correctly */
00140 //  u32Status
00141 //      = u32fnSMValidateCurrentState((uint16_t*)cau16SMInitValidInputStates,
00142 //                                    N_ELEMENTS(cau16SMInitValidInputStates));
00143 //  if(CLEAR == u32Status)
00144 //  {
00145 //    /* Here comes the real contents of the state */
00146 //    LOCK_STATE_EXECUTION();
00147 //    
00148 //    /* Unlock the state */
00149 //    UNLOCK_STATE_EXECUTION();
00150 //  }
00151 //  else
00152 //  {
00153 //    /* Leave */
00154 //  }
00155 //
00156 //  /* Set the next state */
00157 //  if(CLEAR == u32Status)
00158 //  {
00159 //    vfnSMWriteNextState(SM_APPLICATION_START);
00160 //  }
00161 //  else
00162 //  {
00163 //    vfnSMWriteNextState(SM_STATE_ERROR);
00164 //  }
00165 //  return (u32Status);
00166 //}
00167 /*
00168  ******************************************************************************
00169  *
00170  *  End of file.
00171  *
00172  ******************************************************************************
00173  */